package com.yc.project2_knn.group2;

import com.yc.project2_knn.EuclideanDistanceCalculator;
import com.yc.project2_knn.bean.BankMarketing;
import com.yc.project2_knn.bean.Distance;
import com.yc.project2_knn.bean.Sample;
import lombok.Data;

import java.util.List;
import java.util.concurrent.CountDownLatch;

@Data
public class GroupDiatanceTask implements Runnable{
    private Distance[] distances;
    private Integer startIndex;
    private Integer endIndex;
    private List<BankMarketing> dataSet;
    private Sample example;
    private EuclideanDistanceCalculator edc =new EuclideanDistanceCalculator();
    private CountDownLatch latch;

    public GroupDiatanceTask(Distance[] distances, Integer startIndex, Integer endIndex, List<BankMarketing> dataSet, Sample example) {
        this.distances = distances;
        this.startIndex = startIndex;
        this.endIndex = endIndex;
        this.dataSet = dataSet;
        this.example = example;
    }

    public GroupDiatanceTask(Distance[] distances, Integer startIndex, Integer endIndex, List<BankMarketing> dataSet, Sample example, EuclideanDistanceCalculator edc) {
        this.distances = distances;
        this.startIndex = startIndex;
        this.endIndex = endIndex;
        this.dataSet = dataSet;
        this.example = example;
        this.edc = edc;
    }

    public GroupDiatanceTask(Distance[] distances, Integer startIndex, Integer endIndex, List<BankMarketing> dataSet, Sample example, EuclideanDistanceCalculator edc, CountDownLatch latch) {
        this.distances = distances;
        this.startIndex = startIndex;
        this.endIndex = endIndex;
        this.dataSet = dataSet;
        this.example = example;
        this.edc = edc;
        this.latch = latch;
    }

    //修改距离算法
    public void setEdc(EuclideanDistanceCalculator edc) {
        this.edc = edc;
    }

    @Override
    public void run() {
        //循环从startIndex.到endIndex计算 example 与dataSet中各条数据的距离
        for( int index=startIndex;index<endIndex; index++){
            Sample sample=dataSet.get( index );
//计算sample与测试数据 exampZe 的距离
            distances[index]= new Distance();
            distances[index].setIndex(index);
            distances[index].setDistance(this.edc.calculator(sample,example));
        }
        latch.countDown();
    }

}
